package com.xm9m.web.service.api;

import com.salesmanager.core.business.customer.model.CustomerCriteria;
import com.salesmanager.core.modules.sms.SmsService;
import com.salesmanager.core.business.customer.model.Customer;
import com.salesmanager.core.business.customer.service.CustomerService;
import com.salesmanager.web.entity.SMSMessage;
import com.salesmanager.web.entity.SMSType;
import com.salesmanager.web.utils.SMS.ValidateCode;
import com.xm9m.web.service.api.request.RegisterRequest;
import com.xm9m.web.service.api.response.common.SimpleResponse;
import com.xm9m.web.service.constants.StatusCode;
import com.xm9m.web.service.interfaces.AbstractRestApi;
import com.xm9m.web.service.interfaces.IXm9mResponse;
import com.xm9m.web.service.internal.util.BuildResponseUtil;
import com.xm9m.web.service.utils.GenerateNick;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;


public class CustomerRegister extends AbstractRestApi {

    private static final Logger logger = LoggerFactory.getLogger(CustomerRegister.class);

    @Autowired
    private  CustomerService customerService;

    @Autowired
    private SmsService smsService;


    @Override
    public <T extends IXm9mResponse> T execute(String requestData, String version) {
        Map<String, Object> map;
        SimpleResponse registerResponse;

        RegisterRequest registerRequest = super.json2Bean(requestData, RegisterRequest.class);
        if (registerRequest == null) {
            registerResponse = BuildResponseUtil.buildSimpleResponse(StatusCode.PARAM_WRONG);
            return (T) registerResponse;
        }
        try {
   //         SMSMessage originMessage =(SMSMessage) smsService.getByTelephone(registerRequest.getMobile());
//            SMSMessage newMessage=new SMSMessage();
//            newMessage.setCode(registerRequest.getCaptcha());
//            newMessage.setPhone(registerRequest.getMobile());
//            newMessage.setType(SMSType.CUSTOMER_REGISTER.getValue());
//            map = ValidateCode.validate(originMessage, newMessage);
//
//            String valid = (String) map.get("valid");
//            if ("false".equals(valid)) {
//                registerResponse = BuildResponseUtil.buildSimpleResponse(StatusCode.CAPTCHA_ERROR);
//                return (T) registerResponse;
//            }
            Customer customer = customerService.getByTelphone(registerRequest.getMobile());
            if (customer != null) {
                return (T) BuildResponseUtil.buildSimpleResponse(StatusCode.USER_EXISTS);
            }
            String nick= GenerateNick.generate();

            CustomerCriteria customerCriteria = new CustomerCriteria();
            customerCriteria.setMobilePhone(registerRequest.getMobile());
            customerCriteria.setPassword(registerRequest.getPassword());
            customerCriteria.setNick(nick);
            customerCriteria.setAuthType(1);
            customerCriteria.setChannel(registerRequest.getChannel());

            //save
            boolean b = customerService.saveSignUpUser(customerCriteria);
            if (!b) {
                registerResponse = BuildResponseUtil.buildSimpleResponse(StatusCode.USER_EXISTS);

                return (T) registerResponse;
            }
        }catch (Exception ee){
            logger.error(ee.getMessage(),ee);
            registerResponse = BuildResponseUtil.buildSimpleResponse(StatusCode.UNKNOWN_ERROR);

            return (T) registerResponse;

        }
        registerResponse = BuildResponseUtil.buildSimpleResponse(StatusCode.SUCCESS);
        //删除短信
//        smsService.delete(registerRequest.getMobile());
        return (T) registerResponse;
    }
}